package com.wenlin.customerservice.domain;

import com.netflix.discovery.util.StringUtil;
import org.springframework.util.StringUtils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/***
 *  Created with IntelliJ IDEA.
 *  author:  wenlin
 *  Date:  2018/3/20
 *  Time:  16:29
 *  Description:
 **/
public class Code {
    public static String sendGetReq(String host, String param, String encoding, String decoding) throws Exception{
        String result = "";
        BufferedReader reader = null;
        InputStream inputStream = null;

        try {
            param = param == null ? "" :param;
            param = StringUtils.isEmpty(encoding) ? param : URLEncoder.encode(param, encoding);

            URL url = new URL(host + param);
            URLConnection conn = url.openConnection();
            inputStream = conn.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream,  decoding));

            String line = null;
            while((line = reader.readLine()) != null) {
                result += line;
                System.out.println(result);
            }
        } catch (Exception e) {
            throw e;
        } finally{
            if(reader != null) reader.close();
            if(reader != null) inputStream.close();
        }
        return result;
    }
    public static void main(String[] args) {
        String url= "http://10.1.5.31:8110/?addr=";
        String param = "江苏省无锡市新吴区无锡高新技术产业开发区无锡市新区震泽路18号无锡国家软件园飞鱼座D幢";
        String encoding = "gbk";
        String decoding = "gbk";
        try {
            sendGetReq(url,param,encoding,decoding);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
